package agenda

import java.util.Calendar
import java.util.GregorianCalendar

abstract class Entry(private var _agenda: Agenda, private var _day: Int, private var _subject: String)
  extends Timeable {
  require(_agenda != null)
  require(_day > 0)
  require(_subject != null)
  agenda.addEntry(this)

  def terminate(): Unit = {
    var oldAgenda: Agenda = agenda
    _agenda = null
    oldAgenda.removeEntry(this)
  }

  def isTerminated(): Boolean = {
    agenda == null
  }

  def subject = _subject
  def subject_=(s: String) {
    require(s != null)
    _subject = s
  }


  def day = _day
  def day_(day: Int): Unit = {
    if (!isTerminated()) {
      if (canHaveAsDay(day)) {
        _day = day
      } else if (_day == 0) {
        _day = 1
      }
    }
  }

  def agenda = _agenda

  def canHaveAsDay(day: Int): Boolean = {
    day >= 0
  }

  override def getStartTime(): Long = {
    val referenceDate: Calendar = new GregorianCalendar(2011, 1, 1);
    referenceDate.getTimeInMillis() + this.day * 24 * 60 * 60 * 1000;
  }

  def overlapsWith(other: Entry): Boolean

  def mergeWith(other: Entry): Entry
}